package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.discovery.bean.ArticleDetaisRequest;
import com.rongshu.view.home.activity.SingleArticleDetailsActivity;
import com.rongshu.view.home.listener.OnItemClickListener;
import com.rongshu.view.userInfo.adapter.RecommendAdapter;
import com.rongshu.view.userInfo.entity.Recommend;
import com.rongshu.view.userInfo.iview.RecommendView;
import com.rongshu.view.userInfo.presenter.RecommendPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static com.rongshu.utils.UserInfoUtils.getUserId;

public class RecommendActivity extends BaseActivity<RecommendPresenter> implements RecommendView, OnItemClickListener {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.layout_empty)
    RelativeLayout layout_empty;
    @BindView(R.id.empty_hint)
    TextView emptyHint;

    //manager
    private LinearLayoutManager manager = null;
    //adapter
    private RecommendAdapter adapter = null;
    //map
    private Map<String, Object> map = null;
    private int pageIndex = 1;
    private int pageSize = 10;
    //refresh,load
    private boolean isrefresh = true;
    //intent
    private Intent intent = null;
    private int userId;

    @Override
    protected int initLayout() {
        return R.layout.activity_recommend_view;
    }

    @Override
    protected void initView() {
        //set presenter
        setPresenter(new RecommendPresenter(this), this);
        intent = getIntent();
        if (intent != null) {
            userId = intent.getIntExtra(Constant.USER_ID, 0);
        }
        //设置title
        if (userId == UserInfoUtils.getUserId()) {
            txt_top_title.setText(R.string.my_recommend);
        } else {
            txt_top_title.setText("Ta的推荐");
            emptyHint.setText("Ta还没有推荐过文章~");
        }
        //layoutmanager
        manager = new LinearLayoutManager(this);
        //adapter
        adapter = new RecommendAdapter(this);
        adapter.setOnItemClickListener(this);
        //recyclerview
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        //触发自动刷新
        refreshLayout.autoRefresh();
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                isrefresh = true;
                pageIndex = 1;
                refreshlayout.resetNoMoreData();
                getMyRecommend();
                refreshlayout.finishRefresh(2000, false);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                isrefresh = false;
                pageIndex++;
                getMyRecommend();
                refreshlayout.finishLoadmore(2000, false);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    /**
     * 我的推荐
     */
    private void getMyRecommend() {
        //user_id
        int user_id = getUserId();
        map = new HashMap<>();
        map.put("user_id", userId);
        map.put("page", pageIndex);
        map.put("pagesize", pageSize);
        presenter.getMyRecommend(userId,
                pageIndex, pageSize, Common.encryptionSign(map));
        map.clear();
    }

    @OnClick({R.id.layout_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
        }
    }

    /**
     * 我的推荐
     *
     * @param recommend
     */
    @Override
    public void onMyRecommend(Recommend recommend) {
        if (recommend.getErrCode() == 0) {
            List<Recommend.ResponseBean> list = recommend.getResponse();
            if (adapter != null) {
                if (isrefresh) {
                    refreshLayout.finishRefresh();
                    if (list == null || list.isEmpty()) {
                        refreshLayout.setVisibility(View.GONE);
                        layout_empty.setVisibility(View.VISIBLE);
                    } else {
                        refreshLayout.setVisibility(View.VISIBLE);
                        layout_empty.setVisibility(View.GONE);
                        adapter.setRecommendList(list);
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    if (list == null || list.isEmpty()) {
                        refreshLayout.finishLoadmoreWithNoMoreData();
                    } else {
                        refreshLayout.finishLoadmore();
                        adapter.addRecommendList(list);
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        } else {
            toast(recommend.getErrMsg());
        }
    }

    /**
     * item click
     *
     * @param position
     */
    @Override
    public void onItemClick(int position) {
        if (adapter != null) {
            Recommend.ResponseBean responseBean = adapter.getRecommendList().get(position);
            //发送的实体类
            ArticleDetaisRequest request = new ArticleDetaisRequest();
            request.setContribute_id(responseBean.getContribute_id());
            request.setType(5);
            request.setTopic_id(0);
            intent = new Intent(this, SingleArticleDetailsActivity.class);
            intent.putExtra(Constant.ARTICLE_DETAILS_REQUEST, request);
            startActivity(intent);
        }
    }
}
